package com.sf.osms.dep.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sf.novatar.base.dao.BaseBeanDao;
import com.sf.osms.dep.dao.IRouterTmpDao;
import com.sf.osms.dep.domain.RouterTmp;

@Repository
public class IRouterTmpDaoImpl extends BaseBeanDao<Long,RouterTmp> implements IRouterTmpDao {

	@Override
	public List<RouterTmp> findFailuresByAgentWaybill(String agentWaybill) {
		Session session = this.getSessionFactory().getCurrentSession();
		Query query = session.createQuery("From RouterTmp t where t.agentWaybill = :agentWaybill and t.sfWaybill =:sfWaybill" );
		query.setString("agentWaybill", agentWaybill);
		query.setString("sfWaybill", "");
		return query.list();
	}

	@Override
	public List<RouterTmp> queryWithinTime(Date startDate, String agentWayBill) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RouterTmp.class);
		criteria.add(Restrictions.gt("insertTime", startDate));
		criteria.add(Restrictions.isNull("sfWaybill"));
		criteria.add(Restrictions.eq("agentWaybill",agentWayBill));
		//最早的数据最先处理，后面的数据可以覆盖前面的数据
		criteria.addOrder(Order.asc("insertTime"));
		List<RouterTmp> result = this.findBy(criteria);
		return result;
	}

}
